package com.developer.platform.application.command;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.developer.platform.application.command.cmd.NoticeAdd;
import com.developer.platform.application.command.cmd.NoticeUpdate;
import com.developer.platform.domain.NoticeAggregateRoot;
import com.developer.platform.domain.service.INoticeService;
import com.veeker.core.utils.JsonUtils;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-10
 */
@Component
public class NoticeCmdManager {

    private final INoticeService noticeService;

    public NoticeCmdManager(INoticeService noticeService) {
        this.noticeService = noticeService;
    }

    public void save(NoticeAdd noticeAdd){
        NoticeAggregateRoot noticeAggregateRoot = new NoticeAggregateRoot();
        BeanUtil.copyProperties(noticeAdd, noticeAggregateRoot);
        noticeAggregateRoot.setContents(JSONUtil.toJsonStr(noticeAdd.getContents()));
        if(CollUtil.isNotEmpty(noticeAdd.getUrls())){
            noticeAggregateRoot.setUrls(JSONUtil.toJsonStr(noticeAdd.getUrls()));
        }
        noticeService.save(noticeAggregateRoot);
    }

    public void update(long id, NoticeUpdate noticeUpdate){
        NoticeAggregateRoot noticeAggregateRoot = new NoticeAggregateRoot(id);
        BeanUtil.copyProperties(noticeUpdate, noticeAggregateRoot);
        noticeAggregateRoot.setContents(JsonUtils.toJson(noticeUpdate.getContents()));
        if(CollUtil.isNotEmpty(noticeUpdate.getUrls())){
            noticeAggregateRoot.setUrls(JsonUtils.toJson(noticeUpdate.getUrls()));
        }
        noticeService.update(noticeAggregateRoot);
    }

    public void delete(long id){
        noticeService.delete(id);
    }

}
